package com.bm.dao;

import com.bm.util.FileUtil;
import com.bm.util.JdbcUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * 学生信息Dao层
 */
public class CollegeDao {

    /**
     * 从数据库查找学校名称
     * @return
     */
    public Set<String> selectCollege(){
        Set<String> nameSet = new HashSet<>();//名字字符串集合

        Connection conn = null;//申明数据库连接对象
        Statement stmt = null;//申明数据库执行工具
        ResultSet rs = null;
        String sql = "select college_name from t_college";
        try {
            conn = JdbcUtil.getConnection();//获取数据库连接对象
            stmt = conn.createStatement();//获取执行SQL的工具
            rs = stmt.executeQuery(sql);//执行查询
            while (rs.next()){
                nameSet.add(rs.getString("college_name"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtil.release(conn,stmt,rs);
        }

        return nameSet;
    }

    /**
     * 从数据库查找专业名称
     * @return
     */
    public Set<String> selectMajor(){
        Set<String> nameSet = new HashSet<>();//名字字符串集合

        Connection conn = null;//申明数据库连接对象
        Statement stmt = null;//申明数据库执行工具
        ResultSet rs = null;
        String sql = "select major_name from t_major";
        try {
            conn = JdbcUtil.getConnection();//获取数据库连接对象
            stmt = conn.createStatement();//获取执行SQL的工具
            rs = stmt.executeQuery(sql);//执行查询
            while (rs.next()){
                nameSet.add(rs.getString("major_name"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtil.release(conn,stmt,rs);
        }

        return nameSet;
    }
}
